package com.pycredit.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * TODO: 增加描述
 * 
 * @author yiz
 * @date 2016年5月26日 下午3:13:51
 * @version 1.0.0 
 * @copyright pycredit.cn 
 */
public class FileUtil {
	/**
	* 导入
	* 
	* @param file csv文件(路径+文件)
	* @return
	*/
	public static List<String> importFile(String name) {
		List<String> dataList = new ArrayList<String>();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(
					Thread.currentThread().getContextClassLoader().getResourceAsStream(name), "UTF-8"));
			String line = null;
			while ((line = br.readLine()) != null) {
				dataList.add(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return dataList;
	}

	public static Properties importPro(String fileName) {
		InputStream inputStream = null;
		Properties properties = new Properties();
		try {
			inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); // properties.load(Prop.class.getResourceAsStream(fileName));
			if (inputStream == null)
				throw new IllegalArgumentException("Properties file not found in classpath: " + fileName);
			properties.load(new InputStreamReader(inputStream, "UTF-8"));
		} catch (IOException e) {
			throw new RuntimeException("Error loading properties file.", e);
		} finally {
			if (inputStream != null)
				try {
					inputStream.close();
				} catch (IOException e) {
				}
		}
		return properties;
	}

	public static Properties importAbsPro(String filePath) {
		InputStream inputStream = null;
		Properties properties = new Properties();
		try {
			inputStream = new FileInputStream(filePath);
			properties.load(new InputStreamReader(inputStream, "UTF-8"));
		} catch (IOException e) {
			throw new RuntimeException("Error loading properties file.", e);
		} finally {
			if (inputStream != null)
				try {
					inputStream.close();
				} catch (IOException e) {
				}
		}
		return properties;
	}
}
